package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * ///api/user/wxLogin/updateUser
     * 修改用户信息
     * @param userInfoVo
     * @return
     */
    @PostMapping("/updateUser")
    @GuiGuLogin
    public Result updateUser(@RequestHeader("token")String token, @RequestBody UserInfoVo userInfoVo){
        Long userId = AuthContextHolder.getUserId();
        userInfoService.updateUser(userId, userInfoVo, token);
        return Result.ok();
    }


    /**
     * ///api/user/wxLogin/getUserInfo
     * 获取登录用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    @GuiGuLogin
    public Result<UserInfoVo> getUserInfo(){
        Long userId = AuthContextHolder.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class);
        return Result.ok(userInfoVo);
    }


    /**
     * /api/user/wxLogin/wxLogin/{code}
     * 小程序授权登录
     * @param code
     * @return token
     */
    @GetMapping("/wxLogin/{code}")
    public Result<Map<String, String>> wxLogin(@PathVariable String code){
        Map<String, String> resultMap = userInfoService.wxLogin(code);
        return Result.ok(resultMap);
    }
}
